package com.hontech.bread.ui.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.hontech.bread.R
import com.hontech.bread.ui.dialog.DateTime
import com.hontech.bread.ui.dialog.selectDateTime
import com.hontech.bread.ui.view.LabelTextView
import com.hontech.bread.utils.EmptyEdge
import com.hontech.bread.utils.onClick

class SalesFragment : Fragment() {

    private lateinit var ltStart: LabelTextView
    private lateinit var ltEnd: LabelTextView
    private lateinit var btnStart: Button
    private lateinit var btnEnd: Button
    private lateinit var btnQuery: Button
    private lateinit var mRv: RecyclerView

    private var mStart: DateTime? = null
    private var mEnd: DateTime? = null

    private lateinit var mAdapter: SalesAdapter

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_sales, null)
        initUi(view)
        return view
    }

    private fun initUi(view: View) {
        ltStart = view.findViewById(R.id.id_sales_lt_start_time)
        ltEnd = view.findViewById(R.id.id_sales_lt_end_time)
        btnStart = view.findViewById(R.id.id_sales_btn_start_time)
        btnEnd = view.findViewById(R.id.id_sales_btn_end_time)
        btnQuery = view.findViewById(R.id.id_sales_btn_query)
        mRv = view.findViewById(R.id.id_sales_rv)
        mAdapter = SalesAdapter()

        mRv.adapter = mAdapter
        mRv.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        mRv.edgeEffectFactory = EmptyEdge()

        btnStart.onClick {
            val dt = selectDateTime()
            ltStart.set(dt.format())
            mStart = dt
        }

        btnEnd.onClick {
            val dt = selectDateTime()
            ltEnd.set(dt.format())
            mEnd = dt
        }

        btnQuery.onClick { onQuery() }
    }

    private suspend fun onQuery() {

    }

    private class SalesItem(view: View) : RecyclerView.ViewHolder(view) {

        private val tvName = view.findViewById<TextView>(R.id.id_item_sales_tv_name)
        private val tvQty = view.findViewById<TextView>(R.id.id_item_sales_tv_qty)
        private val tvSales = view.findViewById<TextView>(R.id.id_item_sales_tv_sales)
        private val tvPayment = view.findViewById<TextView>(R.id.id_item_sales_tv_payment)


    }

    private class SalesAdapter : RecyclerView.Adapter<SalesItem>() {


        override fun getItemCount(): Int {
            return 0
        }

        override fun onBindViewHolder(holder: SalesItem, position: Int) {

        }

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SalesItem {
            val view =
                LayoutInflater.from(parent.context).inflate(R.layout.item_sales, parent, false)
            return SalesItem(view)
        }
    }


}















